
package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.st.entity.SwStPlanMonth;
import com.zdkj.subway.st.entity.SwStPlanWeek;
import com.zdkj.subway.st.mapper.SwStPlanMonthMapper;
import com.zdkj.subway.st.service.SwStPlanMonthService;
import com.zdkj.subway.st.service.SwStPlanWeekService;
import com.zdkj.subway.st.util.SubwayStConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@AllArgsConstructor
public class SwStPlanMonthServiceImpl extends ServiceImpl<SwStPlanMonthMapper, SwStPlanMonth> implements SwStPlanMonthService {

    private final SwStPlanMonthMapper swStPlanMonthMapper;

    private final SwStPlanWeekService swStPlanWeekService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchMonthPlan(List<SwStPlanMonth> planMonths) {
        for (SwStPlanMonth planMonth : planMonths) {
            planMonth.setDisplay(false);
            planMonth.setIsDecompose("0");
            planMonth.setCheckSubmit(SubwayStConstants.SUBMIT_STATUS_WAIT);
            planMonth.setCheckStatus(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT);
            ObjectUtils.setDefaultFiledValue(planMonth);
        }
        return saveBatch(planMonths);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decompose(Long id, List<SwStPlanWeek> planWeeks) {
        SwStPlanMonth swStPlanMonth = swStPlanMonthMapper.selectById(id);
        if("1".equals(swStPlanMonth.getIsDecompose())){
            // 如果是已分解不能再分解
            log.warn("Plan ({}) is decomposed state, cannot be decomposed any more.", id);
            return false;
        }
        int decomposeNumber = 0;
        Set<String> decomposedMonth = new HashSet<>();
        if(planWeeks.size()<=0 || planWeeks==null){
            return false;
        }
        //月分解到周 单号后要加01 以此类推
        int i = 1;
        String no = "";
        for (SwStPlanWeek planWeek : planWeeks) {
            if(planWeek.getId() == null){
                // 如果是新增分解数据，设置默认值
                ObjectUtils.setDefaultFiledValue(planWeek);
                planWeek.setPlanMonthId(swStPlanMonth.getId());
                planWeek.setPlanType(swStPlanMonth.getPlanType());
                planWeek.setPlanWeek(planWeek.getWorkCycle());
                planWeek.setWorkTime(swStPlanMonth.getWorkTime());
                //planWeek.setPlanNumberOld(swStPlanMonth.getPlanNumberOld());
                planWeek.setPlanNumberOld(planWeek.getTicketNumber());
                planWeek.setPlanNumberNow(planWeek.getTicketNumber());
                planWeek.setTicketType(swStPlanMonth.getTicketType());
                planWeek.setCheckStatus(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT);
                planWeek.setCheckSubmit(SubwayStConstants.SUBMIT_STATUS_WAIT);
                if(i<10){
                    no = "0"+i;
                }else {
                    no = ""+i;
                }
                planWeek.setNumbers(swStPlanMonth.getNumbers()+no);//单号
                planWeek.setOperator(swStPlanMonth.getOperator());
                i++;
            }
            decomposeNumber += planWeek.getTicketNumber();
            decomposedMonth.add(planWeek.getPlanMonth());
        }
        swStPlanMonth.setDecomposeNumber(decomposeNumber);
        // 已分解月份，多个用逗号分隔
        swStPlanMonth.setDecomposeMonth(String.join(",", decomposedMonth));
        // 计划张数，如果现计划张数不为空则取现计划张数
        int planNumber = swStPlanMonth.getPlanNumberNow() != null ? swStPlanMonth.getPlanNumberNow() : swStPlanMonth.getPlanNumberOld();
        // 如果分解数等于计划数，将状态置为“已分解”
        if(decomposeNumber == planNumber){
            swStPlanMonth.setIsDecompose("1");
        }
        swStPlanMonthMapper.updateById(swStPlanMonth);
        // FIXME 未处理删除操作
        swStPlanWeekService.saveOrUpdateBatch(planWeeks);
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDecompose(Long id, List<SwStPlanWeek> planWeeks) {
        //id是月计划ID
        SwStPlanMonth swStPlanMonth = swStPlanMonthMapper.selectById(id);
        int decomposeNumber = 0;
        Set<String> decomposedMonth = new HashSet<>();
        if(planWeeks.size()<=0 || planWeeks==null){
            return false;
        }
        if(!"".equals(id.toString())){
            swStPlanWeekService.delByPlanMonthId(id);
        }
        for (SwStPlanWeek planWeek : planWeeks) {
            if(planWeek.getId() == null){
                // 如果是新增分解数据，设置默认值
                ObjectUtils.setDefaultFiledValue(planWeek);
                planWeek.setPlanMonthId(swStPlanMonth.getId());
                planWeek.setPlanType(swStPlanMonth.getPlanType());
                planWeek.setPlanWeek(planWeek.getWorkCycle());
                planWeek.setWorkTime(swStPlanMonth.getWorkTime());
                //planWeek.setPlanNumberOld(swStPlanMonth.getPlanNumberOld());
                planWeek.setPlanNumberOld(planWeek.getTicketNumber());
                planWeek.setPlanNumberNow(planWeek.getTicketNumber());
                planWeek.setTicketType(swStPlanMonth.getTicketType());
                planWeek.setCheckStatus(SubwayStConstants.AUDIT_STATUS_NOT_SUBMIT);
                planWeek.setCheckSubmit(SubwayStConstants.SUBMIT_STATUS_WAIT);
            }
            decomposeNumber += planWeek.getTicketNumber();
            decomposedMonth.add(planWeek.getPlanMonth());
        }
        swStPlanMonth.setDecomposeNumber(decomposeNumber);
        // 已分解月份，多个用逗号分隔
        swStPlanMonth.setDecomposeMonth(String.join(",", decomposedMonth));
        // 计划张数，如果现计划张数不为空则取现计划张数
        int planNumber = swStPlanMonth.getPlanNumberNow() != null ? swStPlanMonth.getPlanNumberNow() : swStPlanMonth.getPlanNumberOld();
        // 如果分解数等于计划数，将状态置为“已分解”
        if(decomposeNumber == planNumber){
            swStPlanMonth.setIsDecompose("1");
        }
        swStPlanMonthMapper.updateById(swStPlanMonth);
        // FIXME 未处理删除操作
        swStPlanWeekService.saveOrUpdateBatch(planWeeks);
        return true;
    }
}
